const Audit1 = artifacts.require("Audit1");
const fs = require('fs')



async function getRecords(){
    //获取所有记录，包括记录类型、文件名、文件用户地址、时间戳、块号
    let specificInstance=await Audit1.deployed()
    let recordLength=await specificInstance.getRecordLength()
    content=''
    for(var i=0;i<recordLength;++i){
        let oneRecord=await specificInstance.getRecord(i)
        //处理返回的记录
        let rtype=oneRecord[0]
        let rfname=oneRecord[1]
        let rusraddr=oneRecord[2]
        let rtimestamp=oneRecord[3]
        let rblknum=oneRecord[4]
        if(parseInt(rblknum)>parseInt(1000)){
            content=content+rtype+','+rfname+','+rusraddr+','+rtimestamp+','+rblknum+';\n'

        }
    }
    
    try {
        const data = fs.writeFileSync('./YanMengAuditRecord.txt', content)
        //文件写入成功。
      } catch (err) {
        console.error(err)
      }
    

}

module.exports = async function(callback) {
    // TODO: implement your actions
    // await upload()
    var dt=new Date()
    dt.toLocaleString()
    console.log("get reocrds start at:"+dt)
    await getRecords()
    dt=new Date()
    dt.toLocaleString()
    console.log("all done at:"+dt)
    callback();
  }